<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :model="keyword" :inline="true">
				<el-form-item>
					<el-input v-model="keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="warning" v-on:click="getTableData">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="addHandler">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表v-loading="listLoading"-->
		<el-table border :data="tableDatas" v-loading="listLoading" @selection-change="handleSelectionChange"
				  highlight-current-row style="width: 100%;">
			<!--多选框-->
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="70" label="序号" fixed="left">
			</el-table-column>
			<el-table-column prop="name" label="菜单名称" width="120" fixed="left">
			</el-table-column>
			<el-table-column prop="component" label="组件路径" width="180" sortable>
			</el-table-column>
			<el-table-column prop="url" label="路由地址" width="140">
			</el-table-column>
			<el-table-column prop="icon" label="菜单图标" width="100">
			</el-table-column>
			<el-table-column prop="index" label="菜单排序" width="120">
			</el-table-column>
			<el-table-column prop="parent.name" label="上级菜单" width="320">
			</el-table-column>
			<el-table-column label="操作" align="center" fixed="right" width="300">
				<template scope="scope">
					<el-button size="small" @click="edit(scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="del(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" :disabled="this.sels.length === 0" @click="delBatch">批量删除</el-button>
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
						   :page-sizes="[5, 10, 50]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
						   :total="totals" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增界面-->
		<el-dialog :title="title" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" ref="addForm">
				<el-form-item label="菜单名称" prop="name">
					<el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="组件路径" prop="component">
					<el-input v-model="addForm.component" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="路由地址" prop="url">
					<el-input v-model="addForm.url" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="菜单图标" prop="icon">
					<el-input v-model="addForm.icon" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="菜单排序" prop="index">
					<el-input v-model="addForm.index" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="上级菜单" prop="parentId">
					<el-cascader v-model="addForm.parentId" :options="menuTree"
								 :props="{ checkStrictly: true, value: 'id', label: 'name' }" clearable></el-cascader>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>

export default {
	data() {
		return {
			row: "",
			sels: [], //存储多选记录
			menuTree: [],
			addFormVisible: false,
			title: '',
			addForm: {
				id: null,
				name: '',
				component: '',
				url: '',
				icon: '',
				index: '',
				parentId: '',
				parent: null,
			},
			listLoading: false,
			//查询对象

			keyword: null,

			totals: 0,//总记录条数
			currentPage: 1,//当前页,要传递到后台的
			pageSize: 5, //每页显示多少条
			tableDatas: [], //当前页数据
		}
	},
	methods: {
		//表格多条数据选中事件
		handleSelectionChange(val) {
			/**
			 * val中存放的是所有已选择的行数据集合
			 */
			console.log(val);
			this.sels = [];
			if (val != null && val.length > 0) {
				for (var i = 0; i < val.length; i++) {
					//往sels数组中存值
					this.sels.push(val[i].id);
				}
			}
		},

		//提交新增表单数据
		addSubmit() {
			this.title = "新增用户";
			if (this.addForm.parentId) {
				this.addForm.parentId = this.addForm.parentId[this.addForm.parentId.length - 1];
			}

			// 新增
			if (!this.addForm.id) {
				this.$http.post("/menu", this.addForm).then(result => {
					if (result.data.success) {
						this.$message({
							message: result.data.msg,
							type: 'success'
						});
						this.addFormVisible = false;
						this.getTableData();
					} else {
						this.$message({
							message: result.data.msg,
							type: 'error'
						});
					}
				});
			} else { // 修改
				this.$http.put("/menu/", this.addForm).then(result => {
					if (result.data.success) {
						this.$message({
							message: result.data.msg,
							type: 'success'
						});
						this.addFormVisible = false;
						this.getTableData();
					} else {
						this.$message({
							message: result.data.msg,
							type: 'error'
						});
					}
				});
			}

		},
		//点击编辑
		edit(row) {
			this.addFormVisible = true;//显示编辑框
			this.addForm = Object.assign({}, row);//回显数据
			this.title = "修改"
			this.addForm.parentId= this.menuEcho(this.menuTree, this.addForm.parentId)
			console.log(this.menuEcho(this.menuTree, this.addForm.parentId));
			
		},
		//批量删除
		delBatch() {
			this.$confirm('确定删除这些数据吗？', '请确认', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.$http.patch("/menu/batchDel", this.sels).then(result => {
					if (result.data.success) {
						this.$message({
							type: 'success',
							message: result.data.msg
						});
						this.getTableData();
					} else {
						this.$message({
							type: 'error',
							message: result.data.msg
						});
					}
				});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消操作'
				});
			});
		},

		//删除单条数据
		del(row) {
			this.$confirm('确定删除此条数据吗？', '请确认', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.$http.delete("/menu/" + row.id).then(result => {
					if (result.data.success) {
						this.$message({
							type: 'success',
							message: result.data.msg
						});

						// // 1.判断是否需要回到上一页
						// // 	第一个条件判断当前是否在最后一页
						// //  第二个条件判断总条数减去一条是否所在页为空
						// if (this.totals == totalPage && (this.totals - 1) % this.PageSize == 0 && this.currentPage > 1) {
						// 	this.currentPage = this.currentPage - 1;
						// }
						this.getTableData();
					} else {
						this.$message({
							type: 'error',
							message: result.data.msg
						});
					}
				});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消操作'
				});
			});
		},
		addHandler() {
			this.title = "新增"
			this.addForm = {
				id: null,
				name: '',
				component: '',
				url: '',
				icon: '',
				index: '',
				parentId: '',
				parent: null,
			};
			this.addFormVisible = true;
		},
		//选择第几页时触发
		handleCurrentChange(curentPage) {
			this.currentPage = curentPage;
			this.getTableData();
		},
		//选择每页显示记录条数
		handleSizeChange(pageSize) {
			this.curentPage = 1
			this.pageSize = pageSize
			this.getTableData();
		},
		handleChange(value) {
			console.log(this.addForm.courseTypeId);
		},
		//获取列表数据
		getTableData() {
			let para = {
				"currentPage": this.currentPage,
				"pageSize": this.pageSize,
				"keyword": this.keyword
			};
			this.listLoading = true; //显示加载圈
			//分页查询
			this.$http.post("/menu/page", para).then(result => {
				console.log(result.data.data.total);
				console.log(result.data.data.list);

				this.totals = result.data.data.total;
				this.tableDatas = result.data.data.list;
				this.listLoading = false;  //关闭加载圈
			});
		},
		//获取列表数据
		getMenuTree() {
			this.$http.post("/menu/tree").then(result => {
				this.menuTree = result.data.data;

			});
		},
		// el-cascader递归获取父级id,list 数据列表,id 要查找的父级Id
		// list: 是级联选择器的options集合
		// id : 你要查找的级联关系的最后一级的id
		menuEcho(list, id) {
			let arr = []; // 在递归时操作的数组
			let returnArr = []; // 存放结果的数组
			let depth = 0; // 定义全局层级
			// 定义递归函数
			function childrenEach(childrenData, depthN) {
				for (var j = 0; j < childrenData.length; j++) {
					depth = depthN; // 将执行的层级赋值 到 全局层级
					arr[depthN] = childrenData[j].id;
					if (childrenData[j].id == id) {
						returnArr = arr.slice(0, depthN + 1); //将目前匹配的数组，截断并保存到结果数组，
						break;
					} else {
						if (childrenData[j].children) {
							depth++;
							childrenEach(childrenData[j].children, depth);
						}
					}
				}
				return returnArr;
			}

			return childrenEach(list, depth);
		},
	},
	mounted() {
		this.getTableData();

		this.getMenuTree();
	}
}

</script>

<style scoped></style>
